package com.zsc.edu.testrecyclerview;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.os.Bundle;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

    List<MyItem> mMyItemList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化数据
        initMyItemList();

        RecyclerView recyclerView = findViewById(R.id.recycler);

        // 设置布局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        // 设置布局的方向 默认垂直 ，设置成水平
        //linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(linearLayoutManager);

        // 瀑布流 布局管理器
//        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(
//                2,  // 一行2列
//                StaggeredGridLayoutManager.VERTICAL // 方向垂直
//        );
//        recyclerView.setLayoutManager(staggeredGridLayoutManager);

        // 初始化适配器
        RecyclerViewAdapter recyclerViewAdapter = new RecyclerViewAdapter(mMyItemList);

        // 设置适配器
        recyclerView.setAdapter(recyclerViewAdapter);
    }



    private void initMyItemList(){

        Random rand = new Random();

        for(int i=0; i< 10; ++i){
            //int news, int img, int hot_img, int share_img, int hot, int share
//            MyItem myItem = new MyItem(
//                    // 根据资源id号
//                    getResources().getIdentifier("news_1",
//                            "string",  getPackageName()),
//                    getResources().getIdentifier("img1",
//                            "drawable", getPackageName()),
//                    R.drawable.hot,
//                    R.drawable.share,
//                    888,
//                    99);

            MyItem myItem = new MyItem(
                    // 根据资源id号
                    getResources().getIdentifier("news_"+(i+1),
                            "string",  getPackageName()),
                    getResources().getIdentifier("img"+(i+1),
                            "drawable", getPackageName()),
                    R.drawable.hot,
                    R.drawable.share,
                    rand.nextInt(1000),
                    rand.nextInt(1000));

            // 把每个单项数据，添加list里
            mMyItemList.add(myItem);
        }

    }
}